<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  
  class StatsController extends JController
  {  
    function display(){

      $document         =& JFactory::getDocument();
      $view         = & $this->getView($this->_name, $document->getType());
      $division         = $this->getModel('division', 'divisionmodel');
      $seasondivision   = $this->getModel('seasondivision', 'seasondivisionmodel');
      
      $view_type        = JRequest::getVar('others2');
      $season_id        = $this->get_season_id();
      
	  if($season_id){
        $season_divisions = $seasondivision->get_by_season_id($season_id);
        $divisions        = array();
        foreach($season_divisions as $sd){
          $d = $division->get($sd->division_id);
          $divisions[$d->id] = $d->name;
        }
      
        $division_id      = JRequest::getVar('others');
        if(!$division_id)
          $division_id  = array_shift(array_keys($divisions));
      
        $view->assign('divisions', $divisions);
      
        switch($view_type){
          case "points":
            $this->show_points($season_id, $division_id, $view);
            break;
          case "rebounds":
            $this->show_rebounds($season_id, $division_id, $view);
            break;
          case "assists":
            $this->show_assists($season_id, $division_id, $view);
            break;
          case "steals":
            $this->show_steals($season_id, $division_id, $view);
            break;
          case "blocks":
            $this->show_blocks($season_id, $division_id, $view);
            break;
          default:
            $this->show_points($season_id, $division_id, $view);
        }
	  }
      
    }
    
    private function show_points($season_id, $division_id, $view){
      
      $mplayer          = $this->getModel('player', 'playermodel');
      $mperson          = $this->getModel('person', 'personmodel');
      $pic             = $this->getModel('pic', 'picmodel');
      $player_game_stat = $this->getModel('playergamestat', 'playergamestatmodel');
      
      $players     = array();
      $top_scorers = array();
      $tmp_players = $mplayer->get_by_division_id($division_id);
      
      if($tmp_players && $season_id){
        
        foreach($tmp_players as $player){
      
          $person           = $mperson->get($player->person_id);
          $player->fullname         = "$person->lastname, $person->firstname";
          $player->pic        = $pic->get($player->pic_id);
          $pgs                      = $player_game_stat->get_avg_season_stats($player->id, $season_id);
          if($pgs){
            //field goals
            if($pgs['field_goal_attempts'] > 0)
              $pgs['field_goals_pct']  = number_format($pgs['field_goals']/$pgs['field_goal_attempts'], 2);
            else
              $pgs['field_goals_pct']  = $pgs['field_goals'];
            //3points
            if($pgs['three_point_attempts'] > 0)
              $pgs['three_points_pct']  = number_format($pgs['three_points']/$pgs['three_point_attempts'], 2);
            else
              $pgs['three_points_pct']  = $pgs['three_points'];
            //free throws  
            if($pgs['free_throw_attempts'] > 0)
              $pgs['free_throws_pct']    = number_format($pgs['free_throws']/$pgs['free_throw_attempts'], 2);
            else
              $pgs['free_throws_pct']    = $pgs['free_throws'];

            if(isset($pgs['avg_points']))
              $top_scorers[$player->id] = $pgs['avg_points'];
            $player->player_game_stat = $pgs;
          }
          if(!isset($top_scorers[$player->id]))
            $top_scorers[$player->id] = 0;
          $players[$player->id]     = $player;
          
        }
      }
      arsort($top_scorers);
      
      $view->assign("top_scorers", $top_scorers);
      $view->assign("season_id", $season_id);
      $view->assign("division_id", $division_id);
      $view->assign("players", $players);
      $view->assign("view_type", "points");
      $view->display();
    }

    private function show_rebounds($season_id, $division_id, $view){
      
      $mplayer          = $this->getModel('player', 'playermodel');
      $mperson          = $this->getModel('person', 'personmodel');
      $pic             = $this->getModel('pic', 'picmodel');
      $player_game_stat = $this->getModel('playergamestat', 'playergamestatmodel');
      
      $players        = array();
      $top_rebounders = array();
      $tmp_players    = $mplayer->get_by_division_id($division_id);
      
      if($tmp_players && $season_id){
        
        foreach($tmp_players as $player){
      
          $person           = $mperson->get($player->person_id);
          $player->fullname         = "$person->lastname, $person->firstname";
          $player->pic        = $pic->get($player->pic_id);
          $pgs                      = $player_game_stat->get_avg_season_stats($player->id, $season_id);
          if($pgs){
            $pgs['total_rebounds']    = $pgs['def_rebounds'] + $pgs['off_rebounds'];
            $pgs['avg_rebounds']    = number_format($pgs['total_rebounds']/$pgs['games_played'], 2);

            if(isset($pgs['avg_off_rebounds']) && isset($pgs['avg_def_rebounds']))
              $top_rebounders[$player->id] = $pgs['avg_def_rebounds'] + $pgs['avg_off_rebounds'];
            $player->player_game_stat = $pgs;
          }
          if(!isset($top_rebounders[$player->id]))
            $top_rebounders[$player->id] = 0;
          $players[$player->id]     = $player;          
        }
      }
      
      arsort($top_rebounders);
      $view->assign("top_rebounders", $top_rebounders);
      
      $view->assign("players", $players);
      $view->assign("season_id", $season_id);
      $view->assign("division_id", $division_id);
      $view->assign("view_type", "rebounds");
      $view->display('rebounds');
    }
    
    private function show_assists($season_id, $division_id, $view){
      
      $mplayer          = $this->getModel('player', 'playermodel');
      $mperson          = $this->getModel('person', 'personmodel');
      $pic             = $this->getModel('pic', 'picmodel');
      $player_game_stat = $this->getModel('playergamestat', 'playergamestatmodel');
      
      $players        = array();
      $top_assists    = array();
      $tmp_players    = $mplayer->get_by_division_id($division_id);
      
      if($tmp_players && $season_id){
        
        foreach($tmp_players as $player){
      
          $person           = $mperson->get($player->person_id);
          $player->fullname         = "$person->lastname, $person->firstname";
          $player->pic        = $pic->get($player->pic_id);
          $pgs                      = $player_game_stat->get_avg_season_stats($player->id, $season_id);
          if($pgs){
            if(isset($pgs['avg_assists']))
              $top_assists[$player->id] = $pgs['avg_assists'];
            $player->player_game_stat = $pgs;
          }
          if(!isset($top_assists[$player->id]))
            $top_assists[$player->id] = 0;
          $players[$player->id]     = $player;
        }
      }
      
      arsort($top_assists);
      $view->assign("top_assists", $top_assists);
      
      $view->assign("players", $players);
      $view->assign("season_id", $season_id);
      $view->assign("division_id", $division_id);
      $view->assign("view_type", "assists");
      $view->display('assists');
    }
    
    private function show_steals($season_id, $division_id, $view){
      
      $mplayer          = $this->getModel('player', 'playermodel');
      $mperson          = $this->getModel('person', 'personmodel');
      $pic             = $this->getModel('pic', 'picmodel');
      $player_game_stat = $this->getModel('playergamestat', 'playergamestatmodel');
      
      $players        = array();
      $top_steals     = array();
      $tmp_players    = $mplayer->get_by_division_id($division_id);
      
      if($tmp_players && $season_id){
        
        foreach($tmp_players as $player){
      
          $person           = $mperson->get($player->person_id);
          $player->fullname         = "$person->lastname, $person->firstname";
          $player->pic        = $pic->get($player->pic_id);
          $pgs                      = $player_game_stat->get_avg_season_stats($player->id, $season_id);
          if($pgs){
            if(isset($pgs['avg_steals']))
              $top_steals[$player->id] = $pgs['avg_steals'];
            $player->player_game_stat = $pgs;
          }
          if(!isset($top_steals[$player->id]))
            $top_steals[$player->id] = 0;
          $players[$player->id]     = $player;
        }
      }
      
      arsort($top_steals);
      $view->assign("top_steals", $top_steals);
      
      $view->assign("players", $players);
      $view->assign("season_id", $season_id);
      $view->assign("division_id", $division_id);
      $view->assign("view_type", "steals");
      $view->display('steals');
    }
    
    private function show_blocks($season_id, $division_id, $view){
      
      $mplayer          = $this->getModel('player', 'playermodel');
      $mperson          = $this->getModel('person', 'personmodel');
      $pic             = $this->getModel('pic', 'picmodel');
      $player_game_stat = $this->getModel('playergamestat', 'playergamestatmodel');
      
      $players        = array();
      $top_blocks     = array();
      $tmp_players    = $mplayer->get_by_division_id($division_id);
      
      if($tmp_players && $season_id){
        
        foreach($tmp_players as $player){
      
          $person           = $mperson->get($player->person_id);
          $player->fullname         = "$person->lastname, $person->firstname";
          $player->pic        = $pic->get($player->pic_id);
          $pgs                      = $player_game_stat->get_avg_season_stats($player->id, $season_id);
          if($pgs){
            if(isset($pgs['avg_blocks']))
              $top_blocks[$player->id] = $pgs['avg_blocks'];
            $player->player_game_stat = $pgs;
          }
          if(!isset($top_blocks[$player->id]))
            $top_blocks[$player->id] = 0;
          $players[$player->id]     = $player;
        }
      }
      
      arsort($top_blocks);
      $view->assign("top_blocks", $top_blocks);
      $view->assign("season_id", $season_id);
      $view->assign("division_id", $division_id);
      $view->assign("players", $players);
      $view->assign("view_type", "blocks");
      $view->display('blocks');
    }
    
    private function get_season_id(){
      
      $season       = $this->getModel('season', 'seasonmodel');
      $season_id        = JRequest::getVar('id');
      if(!$season_id){
        if($s = $season->get_latest())
          $season_id = $s->id;
      }
      return $season_id;  
    }
  }
?>
